<template>
  <base-layout>
    <template #searchPanel>
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="top-action">配件类型</div>
          <ul class="select-container type-list">
            <li
              v-for="item in dict"
              :class="item.partsDictId == form.partsDictId ? 'active' : ''"
              :key="item.partsDictId"
              @click="categorySelectHandle(item)"
            >
              {{ item.partsDictValue }}
            </li>
          </ul>
        </el-col>
        <el-col :span="18">
          <div class="top-action">
            <div>配件</div>
            <div style="margin-top: -15px">
              <el-input v-model="form.name" placeholder="配件名称/配件编码" />
              <el-button type="primary" @click="query">搜索</el-button>
            </div>
          </div>
          <div class="select-container">
            <el-checkbox-group v-model="checkboxGroup" :max="max">
              <el-checkbox
                v-for="(item, index) in dataList"
                :key="item.id"
                :label="item.id"
                @change="checkboxChangeHandle(item, index, $event)"
              >
                <el-row :gutter="10">
                  <el-col :span="6">{{ item.partsName }}</el-col>
                  <el-col :span="6">{{ item.partsCode }}</el-col>
                  <el-col :span="6">{{
                    item.partsDictValue | dictFilter('用品属性')
                  }}</el-col>
                  <el-col :span="6">{{ item.unit }}</el-col>
                </el-row>
              </el-checkbox>
            </el-checkbox-group>
          </div>
        </el-col>
      </el-row>
    </template>
    <template #contentPanel>
      <el-row>
        <el-col :span="22">已选择配件</el-col>
      </el-row>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <data-table
          :value="tableData"
          :total-count="tableData.length"
          :has-action="true"
          :table-column-attributes="tableColumnAttributes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :width="160"
          :localPage="true"
        >
          <template #action="{ row }">
            <i
              class="el-icon-delete"
              title="删除"
              style="color: red; margin-left: 20px; cursor: pointer"
              @click="deleteById(row)"
            ></i>
          </template>
        </data-table>
      </el-row>
      <el-row>
        <el-col :span="24" align="right" justify="center">
          <el-button type="success" @click="select">选择配件</el-button>
        </el-col>
      </el-row>
    </template>
  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import { getDict } from '@/utils'
import {
  findAll,
  findPartsType
} from '@/api/after-sale/systemManage/accessories'
import { getStoreId } from '@/utils/index'
import { array } from 'jszip/lib/support'
export default {
  mixins: [baseMixin({})],
  // name: 'SelectAccessories',
  props: {
    checkStrictly: { type: Boolean, default: false },
    storageId: { type: Number, default: getStoreId() },
    supplierId: { type: Number, default: 0 },
    partsIds: { type: Array },
    // 可被勾选的 checkbox 的最大数量
    max: {
      type: Number
    }
  },
  data() {
    return {
      form: {},
      tableColumnAttributes: [
        { name: '配件名称', value: 'partsName', type: 'text' },
        { name: '配件编码', value: 'partsCode', type: 'text' },
        { name: '配件类别', value: 'partsDictValue', type: 'text' },
        { name: '供应商', value: 'supplierName', type: 'text' },
        { name: '配件单位', value: 'unit', type: 'text' },
        { name: '内置个数', value: 'internalNum', type: 'text' }
      ],
      dict: [],
      dataList: [],
      checkboxGroup: [],
      tableData: []
    }
  },
  watch: {
    'form.partsDictId': function(value) {
      if (value) {
        this.query(value)
      }
    },
    checkboxGroup: function(value) {
      if (value) {
        // this.addTable(value)
      }
    }
  },
  methods: {
    // 删除
    deleteById(row) {
      this.checkboxGroup = this.checkboxGroup.map(e => {
        if (e != row.id) {
          return e
        }
      })
    },
    checkboxChangeHandle(product, $index, check) {
      if (check) {
        this.tableData.push(product)
      } else {
        const index = this.tableData.findIndex(item => product.id === item.id)
        this.tableData.splice(index, 1)
      }
    },
    categorySelectHandle(item) {
      this.form.partsDictId = item.partsDictId
      this.query()
    },
    async query() {
      if (this.partsIds && this.partsIds.length > 0) {
        this.form.carPartsId = this.partsIds
      }
      if (!this.form.partsDictId) {
        this.$message.error('请选择配件类型')
        return
      }
      let { data } = await findAll(this.form)
      this.dataList = data
    },
    async findPartsType() {
      if (!this.supplierId) {
        this.supplierId = 0
      }
      let { data } = await findPartsType(this.storageId, this.supplierId)
      this.dict = data
    },
    // 选择配件
    select() {
      this.$emit('partsList', this.tableData)
    },
    // 表格数据添加
    addTable(value) {
      if (value.length === 0) {
        this.tableData = []
      } else {
        let data = []
        value.forEach(e => {
          this.dataList.forEach(g => {
            if (e === g.id) {
              g.alreadyNum = e.stockNum
              data.push(g)
            }
          })
        })
        this.tableData = data
      }
    }
  },
  created() {
    this.findPartsType()
  }
}
</script>

<style lang="scss" scoped>
.top-action {
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  /deep/ {
    .el-input {
      width: auto;
    }
  }
}

.select-container {
  height: 240px;
  margin-top: 4px;
  overflow: auto;
  border: 1px solid #dedede;

  /deep/ {
    .el-checkbox-group {
      padding: 5px 5px 0 5px;
    }

    .el-checkbox {
      display: flex;
      align-items: center;
      margin-bottom: 5px;
      margin-right: 0;
      padding: 5px;
      border: 1px solid #dedede;
    }

    .el-checkbox__input {
      display: block;
      height: 14px;
    }

    .el-checkbox__label {
      flex: 1;
      display: flex;
      align-items: center;

      .el-row {
        width: 100%;
      }

      .el-col {
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }
}
.type-list {
  margin-bottom: 0;
  padding: 0 10px;
  list-style: none;
  border: 1px solid #dedede;

  li {
    height: 30px;
    line-height: 30px;
    text-align: center;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    border-bottom: 1px solid #dedede;
    cursor: pointer;

    &:hover,
    &.active {
      color: #409eff;
    }
  }
}
</style>
